package com.crl.rcdp.sys.controller;

import com.cars.util.global.GlobalReturnCode;
import com.cars.util.json.JsonResult;
import com.cars.util.json.JsonResultUtil;
import com.cars.util.string.StringUtil;
import com.cars.util.validation.DeleteValidGroup;
import com.crl.rcdp.sys.dto.SysResourceDTO;
import com.crl.rcdp.sys.service.SysResourceService;
import com.crl.rcdp.sys.vo.SysResourceSearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MimeTypeUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/6/22 17:43
 */
@RestController
@RequestMapping("/sys/resource")
@Api(tags = "资源管理")
public class SysResourceController {
    @Autowired
    private SysResourceService sysResourceService;

    /**
     * 获取所有资源信息
     */
    @PostMapping("/list")
    @ApiOperation(value = "资源列表")
    public JsonResult list(@RequestBody SysResourceSearchVO sysResourceSearchVO) {
//        return JsonResultUtil.ok(sysResourceService.page(sysResourceSearchVO));
        return null;
    }


    /**
     * 新增资源
     *
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增资源")
    public JsonResult add(@RequestBody SysResourceDTO sysResourceDTO) {
        int flag = sysResourceService.add(sysResourceDTO);
        if (flag == 2) {
            return JsonResultUtil.error("资源代码已存在");
        } else if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }

    /**
     * 新增资源
     *
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改资源")
    public JsonResult update(@RequestBody SysResourceDTO sysResourceDTO) {
        int flag = sysResourceService.update(sysResourceDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }

    /**
     * 删除资源
     *
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除资源")
    public JsonResult delete(@Validated({DeleteValidGroup.class}) @RequestBody SysResourceDTO sysResourceDTO) {
        int flag = sysResourceService.delete(sysResourceDTO.getId());
        if (flag == 2) {
            return JsonResultUtil.error("存在下级节点不能删除");
        } else if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }


    /**
     * 获取所有资源信息
     */
    @PostMapping("/get")
    @ApiOperation(value = "获取单个资源")
    public JsonResult get(@RequestBody SysResourceDTO sysResourceDTO) {
        sysResourceDTO = sysResourceService.get(sysResourceDTO.getId());
        return JsonResultUtil.ok(sysResourceDTO);
    }

    /**
     * 获取资源树
     */
    @PostMapping("/getSysResourceTree")
    @ApiOperation(value = "获取资源树")
    public JsonResult getSysResourceTree(@RequestBody SysResourceDTO sysResourceDTO) {
        return JsonResultUtil.ok(sysResourceService.getSysResourcesTree());
    }

    /**
     * 验证权限代码是否存在
     */
    @PostMapping("/checkResourceCode")
    @ApiOperation(value = "验证资源代码", notes = "验证资源代码", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "requestMap", dataType = "{\"resourceCode\":\"资源代码\"}")
    public JsonResult checkResourceCode(@RequestBody Map<String, String> requestMap) {
        if (StringUtil.isNullOrEmpty(requestMap.get("resourceCode"))) {
            return JsonResultUtil.error(GlobalReturnCode.PARAMS_ERROR.getReturnCode(), "resourceCode 不能为空", null);
        }
        SysResourceDTO sysResourceDTO = sysResourceService.getByResourceCode(requestMap.get("resourceCode"));
        if (sysResourceDTO != null) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error("资源代码重复");
        }
    }
}
